package com.example.jt_manager_web;

import com.example.jt_common.PicUploadResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/pic")
public class PicController {

    @RequestMapping("/upload")
    @ResponseBody
    public PicUploadResult  uploadPic(MultipartFile uploadFile){
//        uploadFile.transferTo(new File());
        //fastDFS
        PicUploadResult result=new PicUploadResult();
        try {
            //去除木马，判断文件是否为图片格式
            BufferedImage image = ImageIO.read(uploadFile.getInputStream());

            result.setHeight(String.valueOf(image.getHeight()));
            result.setWidth(String.valueOf(image.getWidth()));

            uploadFile.transferTo(new File("d://"+uploadFile.getOriginalFilename()+".jpg"));
            result.setUrl("http://e.hiphotos.baidu.com/image/h%3D300/sign=a9e671b9a551f3dedcb2bf64a4eff0ec/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg");
        } catch (IOException e) {
            e.printStackTrace();
            result.setError(1);
        }

        return result;
    }
}
